<?php


namespace catchAdmin\product\controller;


use catchAdmin\product\model\Pack as PackModel;

use catchAdmin\product\request\PackCreateRequest;
use catcher\base\CatchController;
use catcher\base\CatchRequest as Request;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\Code;
use catcher\Utils;
use think\facade\Db;

class Pack extends CatchController
{
    protected $pack;

    public function __construct(PackModel $pack)
    {
        $this->pack = $pack;
    }

    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->pack->getList($user['tenant_id']));
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function getPack(CatchAuth $auth)
    {
        $user = $auth->user();
        $list = $this->pack->getAttrList($user['tenant_id']);
        return CatchResponse::success($list->toArray(), '查询成功');
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,PackCreateRequest $request)
    {
        $params = $request->param();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        $params['packing_id'] = date('YmdHis').$user['tenant_id'];
        $params['barcode'] = date('YmdHis').rand(1000,9999);

        $this->pack->storeBy($params);
        return CatchResponse::success('', '新建成功');
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        return CatchResponse::fail('包装创建后，无法修改');

        $params = $request->filterEmptyField()->param();
        $id = $params['id'];
        $this->pack->updateBy($id, $params);
        return CatchResponse::success('', '编辑成功');
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        return CatchResponse::fail('包装创建后，无法删除');

        $id = $request->post('id');
        $ids = Utils::stringToArrayBy($id);
        foreach ($ids as $_id) {
            $this->pack->deleteBy($_id);
        }
        return CatchResponse::success('', '删除成功');
    }
}